<?php
namespace App\Services;
/**
 * Created by PhpStorm.
 * User: SCF
 * Date: 2018/10/18
 * Time: 11:05
 */
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;

class BaseService
{
    /**
     * 第三方 api http 请求
     * @param string $url 请求地址
     * @param string $method 请求api的方法，get，post，patch 等
     * @param array $data 数据，body数据，param 数据等
     * @param array $header 头部信息
     * @param bool $json 请求数据是否为json串
     * @throws \Exception
     * @return object $response
     */
    public static function request($url, $method, $data, $header= [], $json =  false)
    {
        $client = new Client(['timeout' => 10.0]);
        $options = ['verify' => false];
        $method = strtoupper($method);
        if (!empty($data)) {
            if ($json) {
                $key = 'json';
            } elseif ($method === 'GET') {
                $key = 'query';
            } else {
                $key = 'form_params';
            }

            $options[$key] = $data;
        }

        if (!empty($header)) {
            $options['headers'] = $header;
        }

        try {
            return $client->request($method, $url, $options);
        } catch (RequestException $e) {
            if (!$e->hasResponse()) {
                error_exit($e->getCode(), $e->getMessage());
            }
            return $e->getResponse();
        }
    }

    /**
     * @param $url 请求网址
     * @param bool $params 请求参数
     * @param int $ispost 请求方式
     * @param int $https https协议
     * @return bool|mixed
     */
    public static function curl($url, $params = false, $ispost = 0, $https = 0)
    {
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($https) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
        }
        if ($ispost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($params) {
                if (is_array($params)) {
                    $params = http_build_query($params);
                }
                curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }

        $response = curl_exec($ch);

        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        return $response;
    }


}